/* eslint-disable no-param-reassign */
import React, { useEffect, useState } from 'react'
import { Divider, Descriptions, Alert, Table, Row } from 'antd'
import styles from './loading.less'
import loadingImg from '@/assets/loading.png'

const TianLangXingReport = props => {
  const titleStyle = { fontSize: 35, fontFamily: 'cursive' }
  const dividerStyle = { marginTop: 30, marginBottom: 30 }

  // if (!props?.tlxData?.resp_data) return <h3>暂无数据</h3>
  if (!props?.tlxData?.resp_data)
    return (
      <div className={styles.loading}>
        <img src={loadingImg} />
      </div>
    )

  const reportInfo = props?.tlxData
  const {
    base_info: baseInfo,
    hit_risk_tagging: hitRisk,
    score_norm_explain: scoreNorm,
    ident_info_dict: identInfo,
    risk_list_check: riskCheck,
    personal_loan_demand: personalLoan,
    personal_loan_s: personalLoanS,
    personal_loan_f: personalLoanF,
    personal_loan_h: personalLoanRepay,
    personal_overdue_history: personalOverdueHistory,
    rent_history: rentHistory,
    relevance_risk_check: relevanceCheck,
    court_risk_info_list: courtRiskList
  } = reportInfo.resp_data

  // 风险建议
  const showAlert = score => {
    let status = ''
    const alertMsg = {
      reject: {
        message: '建议拒绝',
        type: 'warning'
      },
      check: {
        message: '建议审核',
        type: 'info'
      },
      resolve: {
        message: '建议通过',
        type: 'success'
      }
    }

    if (score >= 0 && score <= 450) {
      status = 'reject'
    }
    if (score > 450 && score <= 530) {
      status = 'check'
    }
    if (score > 530 && score <= 650) {
      status = 'resolve'
    }
    return (
      <Alert
        message={alertMsg[status].message}
        type={alertMsg[status].type}
        showIcon
        style={{ width: 400 }}
      />
    )
  }

  // 风险标注
  const hitRiskMap = {
    命中新增风险特征规则: '命中报告中未展示的内容，数据的模型评分较低则',
    多头查询次数多: '近3个月申请网贷的查询次数较多',
    身份证和手机号关联数据多:
      '用户的身份证号码下注册了多个手机号，或手机号码被多个身份证号码注册',
    号码异常: '手机状态异常、查无此号或注销等异常',
    命中风险关注名单: '命中信贷高风险或租赁关注类名单库',
    近半年内存在逾期记录: '近半年内存在多机构逾期或存在逾期未结清账单',
    司法案件匹配度高:
      '用户历史上有过失信、犯罪、纠纷等严重影响信用记录的法院案件记录审核须谨慎',
    近1个月扣款失败次数较多: '近1个月代扣还款失败次数较多',
    年龄过大: '年龄过大',
    年龄过小: '年龄过小',
    近1周申请租赁平台较多: '近1周查询过用户的租赁平台个数较多',
    号码使用时间较短: '号码使用时间短',
    三要素验证不一致: '姓名、身份证、手机号三要素验证不一致',
    二要素验证不一致: '姓名、身份证验证不一致',
    近1个月网贷平台申请频繁: '近1个月网络助贷平台申请次数较多'
  }

  const fxbzData = hitRisk.map((name, index) => {
    return {
      id: index + 1,
      name,
      desc: hitRiskMap[name]
    }
  })

  const columns = [
    {
      title: '风险点',
      dataIndex: 'name',
      align: 'center'
    },
    {
      title: '详解',
      dataIndex: 'desc',
      align: 'center'
    }
  ]

  // 身份信息核验（运营商在网时长）
  const showOnline = time => {
    const timeOnlineMap = {
      1: '[0,3）',
      2: '[3,6）',
      3: '[6,12）',
      4: '[12,24）',
      5: '[24,+)',
      0: '查无此号或已注销',
      // -1: '不支持该运营商',
      999: '手机状态异常（注:1、销号6月以上；2、携号转网；3、未放出去的号码）；非本网手机号码'
    }
    return timeOnlineMap[time] ?? '不支持该运营商'
  }

  // 风险名单检测（0未命中；1命中）
  const riskOnCheck = risk => {
    return risk ? '命中' : '未命中'
  }

  // 近期贷款需求
  const personalLoanDemandColumns = [
    {
      title: '名称',
      dataIndex: 'name',
      align: 'center'
    },
    {
      title: '近7天',
      dataIndex: 'd7',
      align: 'center'
    },
    {
      title: '近30天',
      dataIndex: 'm1',
      align: 'center'
    },
    {
      title: '近90天',
      dataIndex: 'm3',
      align: 'center'
    }
  ]
  const personalLoanDemandData = [
    {
      id: 1,
      name: '申请查询总数',
      d7: '',
      m1: '',
      m3: ''
    },
    {
      id: 2,
      name: '一般消费分期平台',
      d7: '',
      m1: '',
      m3: ''
    },
    {
      id: 3,
      name: '信用卡',
      d7: '',
      m1: '',
      m3: ''
    },
    {
      id: 4,
      name: '大型消费金融公司',
      d7: '',
      m1: '',
      m3: ''
    },
    {
      id: 5,
      name: 'P2P平台',
      d7: '',
      m1: '',
      m3: ''
    },
    {
      id: 6,
      name: '小额贷款公司',
      d7: '',
      m1: '',
      m3: ''
    },
    {
      id: 7,
      name: '其他类型公司',
      d7: '',
      m1: '',
      m3: ''
    }
  ]

  // 组装近期贷款需求数据
  personalLoanDemandData.forEach(demand => {
    switch (demand.name) {
      case '申请查询总数':
        demand.d7 = personalLoan.d7_apply_setup_time
        demand.m1 = personalLoan.m1_apply_setup_time
        demand.m3 = personalLoan.m3_apply_setup_time
        break
      case '一般消费分期平台':
        demand.d7 = personalLoan.d7_apply_setup_time_ybxffq
        demand.m1 = personalLoan.m1_apply_setup_time_ybxffq
        demand.m3 = personalLoan.m3_apply_setup_time_ybxffq
        break
      case '信用卡':
        demand.d7 = personalLoan.d7_apply_setup_time_xyk
        demand.m1 = personalLoan.m1_apply_setup_time_xyk
        demand.m3 = personalLoan.m3_apply_setup_time_xyk
        break
      case '大型消费金融公司':
        demand.d7 = personalLoan.d7_apply_setup_time_dxxfjr
        demand.m1 = personalLoan.m1_apply_setup_time_dxxfjr
        demand.m3 = personalLoan.m3_apply_setup_time_dxxfjr
        break
      case 'P2P平台':
        demand.d7 = personalLoan.d7_apply_setup_time_p2pwd
        demand.m1 = personalLoan.m1_apply_setup_time_p2pwd
        demand.m3 = personalLoan.m3_apply_setup_time_p2pwd
        break
      case '小额贷款公司':
        demand.d7 = personalLoan.d7_apply_setup_time_xedkgs
        demand.m1 = personalLoan.m1_apply_setup_time_xedkgs
        demand.m3 = personalLoan.m3_apply_setup_time_xedkgs
        break
      case '其他类型公司':
        demand.d7 = personalLoan.d7_apply_setup_time_other
        demand.m1 = personalLoan.m1_apply_setup_time_other
        demand.m3 = personalLoan.m3_apply_setup_time_other
        break

      default:
        break
    }
  })

  // 履约情况
  const repayLoadColumns = [
    {
      title: '名称',
      dataIndex: 'name',
      align: 'center'
    },
    {
      title: '近1个月',
      dataIndex: 'm1',
      align: 'center'
    },
    {
      title: '近3个月',
      dataIndex: 'm3',
      align: 'center'
    },
    {
      title: '近6个月',
      dataIndex: 'm6',
      align: 'center'
    },
    {
      title: '近12个月',
      dataIndex: 'm12',
      align: 'center'
    },
    {
      title: '近24个月',
      dataIndex: 'm24',
      align: 'center'
    }
  ]
  const repayLoadData = [
    {
      id: 1,
      name: '履约次数',
      m1: '',
      m3: '',
      m6: '',
      m12: '',
      m24: ''
    },
    {
      id: 2,
      name: '履约金额',
      m1: '',
      m3: '',
      m6: '',
      m12: '',
      m24: ''
    },
    {
      id: 3,
      name: '还款异常次数',
      m1: '',
      m3: '',
      m6: '',
      m12: '',
      m24: ''
    }
  ]

  repayLoadData.forEach(repay => {
    switch (repay.name) {
      case '履约次数':
        repay.m1 = personalLoanRepay.repay_succ1
        repay.m3 = personalLoanRepay.repay_succ3
        repay.m6 = personalLoanRepay.repay_succ6
        repay.m12 = personalLoanRepay.repay_succ12
        repay.m24 = personalLoanRepay.repay_succ24
        break
      case '履约金额':
        repay.m1 = personalLoanRepay.repay_money1
        repay.m3 = personalLoanRepay.repay_money3
        repay.m6 = personalLoanRepay.repay_money6
        repay.m12 = personalLoanRepay.repay_money12
        repay.m24 = personalLoanRepay.repay_money24
        break
      case '还款异常次数':
        repay.m1 = personalLoanRepay.repay_fail1
        repay.m3 = personalLoanRepay.repay_fail3
        repay.m6 = personalLoanRepay.repay_fail6
        repay.m12 = personalLoanRepay.repay_fail12
        repay.m24 = personalLoanRepay.repay_fail24
        break

      default:
        break
    }
  })

  // 金额区间描述
  const moneyOverdueMap = {
    0: '0',
    1: '（0-500）',
    2: '【500-1000）',
    3: '【1000-2000）',
    4: '【2000-3000）',
    5: '【3000-5000）',
    6: '【5000-10000）',
    7: '【10000-20000）',
    8: '【20000-30000）',
    9: '【30000-50000）',
    10: '【50000-100000）',
    11: '【100000-200000）',
    12: '【200000-300000）',
    13: '【300000-500000）',
    14: '【500000-1000000）',
    15: '【1000000-∞）'
  }

  // 历史租赁记录
  const rentHistoryColumns = [
    {
      title: '名称',
      dataIndex: 'name',
      align: 'center'
    },
    {
      title: '近7天',
      dataIndex: 'd7',
      align: 'center'
    },
    {
      title: '近1个月',
      dataIndex: 'm1',
      align: 'center'
    },
    {
      title: '近3个月',
      dataIndex: 'm3',
      align: 'center'
    },
    {
      title: '近6个月',
      dataIndex: 'm6',
      align: 'center'
    },
    {
      title: '近12个月',
      dataIndex: 'm12',
      align: 'center'
    }
  ]
  const rentHistoryData = [
    {
      id: 1,
      name: '租赁机构申请数',
      d7: '',
      m1: '',
      m3: '',
      m6: '',
      m12: ''
    },
    {
      id: 2,
      name: '租赁申请次数',
      d7: '',
      m1: '',
      m3: '',
      m6: '',
      m12: ''
    }
  ]

  rentHistoryData.forEach(rent => {
    if (rent.name === '租赁机构申请数') {
      rent.d7 = rentHistory.d7_apply_agency_time
      rent.m1 = rentHistory.m1_apply_agency_time
      rent.m3 = rentHistory.m3_apply_agency_time
      rent.m6 = rentHistory.m6_apply_agency_time
      rent.m12 = rentHistory.m12_apply_agency_time
    } else if (rent.name === '租赁申请次数') {
      rent.d7 = rentHistory.d7_apply_time
      rent.m1 = rentHistory.m1_apply_time
      rent.m3 = rentHistory.m3_apply_time
      rent.m6 = rentHistory.m6_apply_time
      rent.m12 = rentHistory.m12_apply_time
    }
  })

  const courtRistColumns = [
    {
      title: '序号',
      // dataIndex: 'name',
      align: 'center',
      render: (text, record, index) => `${index + 1}`
    },
    {
      title: '审结日期',
      dataIndex: 'sort_time_string',
      align: 'center'
    },
    {
      title: '类型',
      dataIndex: 'data_type',
      align: 'center'
    },
    {
      title: '摘要说明',
      dataIndex: 'summary',
      align: 'center'
    },
    {
      title: '匹配度',
      dataIndex: 'compatibility',
      align: 'center'
    }
    // ,
    // {
    //   title: '文书详情',
    //   dataIndex: 'name',
    //   align: 'center'
    // }
  ]

  // 显示产品类别
  const showGoodsType = type => {
    const goodsType = {
      0: '其他',
      1: '游戏电玩',
      2: '娱乐影音',
      3: '电脑平板',
      4: '手机优选'
    }
    return goodsType[type] || '其他'
  }

  // 0新品 - 1二手
  const showGoodsStatus = status => {
    return !status ? '新品' : '二手'
  }

  useEffect(() => {
    console.log('TianLangXingReport')
  }, [])

  return (
    <>
      {/* <Divider style={titleStyle}>天狼星报告星耀版</Divider> */}
      <Divider orientation="left" style={dividerStyle}>
        基本信息
      </Divider>
      <Descriptions bordered column={4}>
        <Descriptions.Item label="姓名">{baseInfo.name}</Descriptions.Item>
        <Descriptions.Item label="年龄">{baseInfo.age}</Descriptions.Item>
        <Descriptions.Item label="身份证号码">
          {baseInfo.ident_number}
        </Descriptions.Item>
        <Descriptions.Item label="手机号码">{baseInfo.phone}</Descriptions.Item>
        <Descriptions.Item label="户籍">
          {baseInfo.ident_number_address}
        </Descriptions.Item>
        <Descriptions.Item label="号码归属地">
          {baseInfo.phone_address}
        </Descriptions.Item>
        <Descriptions.Item label="产品类别">
          {showGoodsType(baseInfo.goods_type)}
        </Descriptions.Item>
        <Descriptions.Item label="新旧">
          {showGoodsStatus(baseInfo.status)}
        </Descriptions.Item>
        <Descriptions.Item label="总租金">
          {baseInfo.total_rent <= 0 ? '-' : baseInfo.total_rent}
        </Descriptions.Item>
        <Descriptions.Item label="总期数">
          {baseInfo.total_periods <= 0 ? '-' : baseInfo.total_periods}
        </Descriptions.Item>
        <Descriptions.Item label="官方价格">
          {baseInfo.price <= 0 ? '-' : baseInfo.price}
        </Descriptions.Item>
        <Descriptions.Item label="性别">{baseInfo.gender}</Descriptions.Item>
      </Descriptions>

      <Divider orientation="left" style={dividerStyle}>
        风险建议
      </Divider>
      {/* 分值在【0，650】之间，得分越低，风险越高：【0，450】建议拒绝；（450，530】建议审核；（530，650】，建议通过。 */}
      {showAlert(scoreNorm)}
      <Divider orientation="left" style={dividerStyle}>
        风险标注
      </Divider>
      <Table
        rowKey="id"
        dataSource={fxbzData}
        columns={columns}
        bordered
        pagination={false}
      />

      <Divider orientation="left" style={dividerStyle}>
        身份信息核验
      </Divider>
      <Descriptions bordered layout="vertical">
        <Descriptions.Item label="实名核验">
          {/* 实名核验 "01": "不一致", "02": "一致" */}
          {identInfo.identity_two_elements === '01' ? '不一致' : '一致'}
        </Descriptions.Item>
        <Descriptions.Item label="运营商核验">
          {/* 运营商核验 "01": "不一致", "02": "一致" */}
          {identInfo.network_triple_elements === '01' ? '不一致' : '一致'}
        </Descriptions.Item>
        <Descriptions.Item label="运营商在网时长">
          {showOnline(identInfo.time_online)}
        </Descriptions.Item>
      </Descriptions>

      <Divider orientation="left" style={dividerStyle}>
        风险名单
      </Divider>
      <Descriptions bordered layout="vertical" column={4}>
        <Descriptions.Item label="信用租赁风险名单">
          {riskOnCheck(riskCheck.credit_rent)}
        </Descriptions.Item>
        <Descriptions.Item label="特殊关注名单">
          {riskOnCheck(riskCheck.result_xd)}
        </Descriptions.Item>
        <Descriptions.Item label="犯罪通缉名单">
          {riskOnCheck(riskCheck.idcard_hit_fztj)}
        </Descriptions.Item>
        <Descriptions.Item label="虚拟号码库">
          {riskOnCheck(riskCheck.phone_hit_xjhm)}
        </Descriptions.Item>
        <Descriptions.Item label="通信小号库">
          {riskOnCheck(riskCheck.phone_hit_txxh)}
        </Descriptions.Item>
        <Descriptions.Item label="归属地位于高风险集中地区">
          {riskOnCheck(riskCheck.census_register_high_risk_area)}
        </Descriptions.Item>
        <Descriptions.Item label="车辆租赁违约名单">
          {riskOnCheck(riskCheck.idcard_hit_clzlwy)}
        </Descriptions.Item>
        <Descriptions.Item label="故意违章乘车名单">
          {riskOnCheck(riskCheck.idcard_hit_clzlwy)}
        </Descriptions.Item>
      </Descriptions>

      <Divider orientation="left" style={dividerStyle}>
        查询次数
      </Divider>
      <Table
        rowKey="id"
        dataSource={personalLoanDemandData}
        columns={personalLoanDemandColumns}
        bordered
        pagination={false}
      />

      <Divider orientation="left" style={dividerStyle}>
        历史借贷行为
      </Divider>
      <Row style={{ marginBottom: 15 }}>近12个月申请情况</Row>
      <Descriptions bordered layout="vertical" column={4}>
        <Descriptions.Item label="消费分期类申请机构数">
          {personalLoanS.consumer_apply_mechanism_number}
        </Descriptions.Item>
        <Descriptions.Item label="网络贷款类申请机构数">
          {personalLoanS.network_loan_apply_mechanis_mnumber}
        </Descriptions.Item>
        <Descriptions.Item label="近1个月申请次数">
          {personalLoanS.apply_time1}
        </Descriptions.Item>
        <Descriptions.Item label="近3个月申请次数">
          {personalLoanS.apply_time3}
        </Descriptions.Item>
        <Descriptions.Item label="近6个月申请次数">
          {personalLoanS.apply_time6}
        </Descriptions.Item>
        <Descriptions.Item label="近12个月申请次数">
          {personalLoanS.apply_time12}
        </Descriptions.Item>
        <Descriptions.Item label="距离最近一次申请日期已有(天)">
          {personalLoanS.last_apply_time_distance}
        </Descriptions.Item>
        <Descriptions.Item label="最近一次申请日期">
          {personalLoanS.last_apply_time}
        </Descriptions.Item>
      </Descriptions>

      <Row style={{ marginTop: 20, marginBottom: 20 }}>近24个月放款情况</Row>
      <Descriptions bordered layout="vertical" column={5}>
        <Descriptions.Item label="消费分期类放款机构数">
          {personalLoanF.cflenders}
        </Descriptions.Item>
        <Descriptions.Item label="网络贷款类放款机构数">
          {personalLoanF.nllenders}
        </Descriptions.Item>
        <Descriptions.Item label="最近一次放款时间">
          {personalLoanF.lend_time}
        </Descriptions.Item>
        <Descriptions.Item label="距离最近一次放款日期已有(天)" span={2}>
          {personalLoanF.lend_time_distance}
        </Descriptions.Item>
        <Descriptions.Item label="近1个月放款次数">
          {personalLoanF.lend_number1}
        </Descriptions.Item>
        <Descriptions.Item label="近3个月放款次数">
          {personalLoanF.lend_number3}
        </Descriptions.Item>
        <Descriptions.Item label="近6个月放款次数">
          {personalLoanF.lend_number6}
        </Descriptions.Item>
        <Descriptions.Item label="近12个月放款次数">
          {personalLoanF.lend_number12}
        </Descriptions.Item>
        <Descriptions.Item label="近24个月放款次数">
          {personalLoanF.lend_number24}
        </Descriptions.Item>
      </Descriptions>

      <Row style={{ marginTop: 20, marginBottom: 20 }}>履约情况</Row>
      <Table
        rowKey="id"
        dataSource={repayLoadData}
        columns={repayLoadColumns}
        bordered
        pagination={false}
      />

      <Divider orientation="left" style={dividerStyle}>
        历史逾期记录
      </Divider>
      {/* <Row style={{ marginTop: 20, marginBottom: 20 }}>近24个月放款情况</Row> */}
      <Descriptions bordered layout="vertical">
        <Descriptions.Item label="逾期机构总数">
          {personalOverdueHistory.overdue_mechanism_number}
        </Descriptions.Item>
        <Descriptions.Item label="逾期总次数">
          {personalOverdueHistory.counts}
        </Descriptions.Item>
        <Descriptions.Item label="逾期总金额（元）">
          {moneyOverdueMap[personalOverdueHistory.overdue_money]}
        </Descriptions.Item>
      </Descriptions>

      <Divider orientation="left" style={dividerStyle}>
        历史租赁行为
      </Divider>
      <Table
        rowKey="id"
        dataSource={rentHistoryData}
        columns={rentHistoryColumns}
        bordered
        pagination={false}
      />

      <Divider orientation="left" style={dividerStyle}>
        关联风险检测
      </Divider>
      <Descriptions bordered layout="vertical">
        <Descriptions.Item label="近3个月身份证关联手机号数">
          {relevanceCheck.m3_idcard_to_phone_time}
        </Descriptions.Item>
        <Descriptions.Item label="近3个月手机号关联身份证数">
          {relevanceCheck.m3_phone_to_idcard_time}
        </Descriptions.Item>
      </Descriptions>

      <Divider orientation="left" style={dividerStyle}>
        法院风险信息
      </Divider>
      <Table
        rowKey="id"
        dataSource={courtRiskList}
        columns={courtRistColumns}
        bordered
        pagination={false}
      />
      <Row style={{ marginTop: 15, fontSize: 12 }}>
        匹配度：匹配度是一个概率（范围为0~1），匹配度为0，则完全不匹配；匹配度为1，则完全匹配。通常，匹配度达到0.8及以上，相似度已经很高，基本上可以确定是这个人；匹配度在0.5及以下，说明可以关联到部分信息，但是相似度较低，大概率不是这个人；匹配度在0.5-0.8时，需根据用户的风控策略，做取舍。
      </Row>
    </>
  )
}

export default TianLangXingReport
